<?php

class PageAction extends Action 
{
	public function _initialize()
	{
		$this->assign('jumpUrl',__URL__);
 		if(!isset($_SESSION['adminid']))
 		{ 
 			 $url=__APP__.'/Index/login';
 			 redirect($url);return;
 		}		
	}       
    public function index()
    {
		$Page=D("Page");
		$count=$Page->count();
		import("ORG.Util.Page");
		$listRows=10;
		$p=new page($count,$listRows);
		$list=$Page->findAll(null,'*','addtime desc',$p->firstRow.','.$p->listRows);
		$pageifno=$p->show();
		if ($list!==false) 
		{
			$this->assign('page',$pageifno);
			$this->assign('list',$list);			
		}
    				
		$this->display();
    }
    public function add()
    {
    	if(!$this->isPost())
    	{
    		$this->display();
    	}
    	else
    	{
    		$Page=D("Page");
    		if(!$Page->create())
    		{
    			$this->error($Page->getError());return;
    		}
    		if(!$Page->add())
    		{ 		
    			$this->error(L('_INSERT_FAIL_'));return;
    		}
    		$this->assign('jumpUrl',__URL__);
    		$msg="页面内容添加成功";
    		$this->success($msg);
    	}
    }
    public function edit()
    {
    	if(!$this->isPost())
    	{
			$id=isset($_GET['id'])?(int)($_GET['id']):0;
			if (!$id)
			{
				$this->error(L('_SELECT_NOT_EXIST_'));return;
			}
			$Page=D("Page");
			$temp=$Page->find($id);
			if (!$temp)
			{
					$this->error(L('_SELECT_NOT_EXIST_'));return;
			}
			    					
			$this->assign('vo',$temp);
			$this->display();			    			
    	}
    	else
    	{
			$id=isset($_POST['id'])?(int)($_POST['id']):0;
			$title=isset($_POST['title'])?trim($_POST['title']):'';
			$url=isset($_POST['url'])?trim($_POST['url']):'';
			$htmlbody=isset($_POST['htmlbody'])?trim($_POST['htmlbody']):'';
			$orderid=isset($_POST['orderid'])?(int)($_POST['orderid']):'';
			
			$data=array(
				'id'=>$id,
				'title'=>$title,
				'url'=>$url,
				'orderid'=>$orderid,
				'htmlbody'=>$htmlbody,
			);
			$Page=D("Page");
			if(!$Page->create())
			{
				$this->error($Page->getError());return;
			}
			if(!$Page->save())
			{
				$this->error(L('_UPDATE_FAIL_'));return;
			}
			$this->assign('jumpUrl',__URL__);
			$msg="页面内容修改成功";
			$this->success($msg);			
    	}    	
    }
	public function del()
	{
		$id=isset($_GET['id'])?(int)($_GET['id']):0;
		if (!$id){ $this->error(L('_SELECT_NOT_EXIST_'));return;}
		$Page=D("Page");
		$temp=$Page->find($id);
		if (!$temp){ $this->error(L('_SELECT_NOT_EXIST_'));return;}
		if(!$Page->deleteById($id))
		{
			$this->error(L('_DELETE_FAIL_'));return;
		}
		$this->redirect();
	}	
}
?>